package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.SetmealCheckgroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealCheckgroup;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealCheckgroupMapper setmealCheckgroupMapper;


    //检查套餐分页展示
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Setmeal> p = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if (queryPageBean.getQueryString()!=null){
            wrapper.like(Setmeal::getCode,queryPageBean.getQueryString()).or()
                    .like(Setmeal::getName,queryPageBean.getQueryString()).or()
                    .like(Setmeal::getHelpCode,queryPageBean.getQueryString());
        }
        Page<Setmeal> setmealPage = setmealMapper.selectPage(p, wrapper);

        return new PageResult(setmealPage.getTotal(),setmealPage.getRecords());
    }
    //添加套餐数据
    @Override
    public Result add(Integer[] checkGroupIds, Setmeal setmeal) {
        //添加套餐
        setmealMapper.insert(setmeal);
        for (Integer checkGroupId : checkGroupIds) {
            setmealMapper.add(setmeal.getId(),checkGroupId);
        }
        return new Result(true,"新增成功");
    }
    //删除套餐
    @Override
    public Result removeById(Integer id) {
        LambdaQueryWrapper<SetmealCheckgroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealCheckgroup::getSetmealId,id);
        setmealCheckgroupMapper.delete(wrapper);
             setmealMapper.deleteById(id);
        return new Result(true,"删除成功");
    }
    //套餐数据回显
    @Override
    public Result findById(Integer id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        return new Result(true,"套餐回显成功",setmeal);
    }
    //修改套餐
    @Override
    public Result modify(Integer[] ids, Setmeal setmeal) {
        setmealMapper.updateById(setmeal);
        LambdaUpdateWrapper<SetmealCheckgroup> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SetmealCheckgroup::getCheckgroupId,ids);
        setmealCheckgroupMapper.delete(wrapper);
        SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
        for (Integer id : ids) {
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(id);
            setmealCheckgroupMapper.insert(setmealCheckgroup);
        }
        return new Result(true,"修改成功");
    }


}
